<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary"
                     @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table :data='cateList'
                  :columns='column'
                  :selection-type='false'
                  :expand-type='false'
                  show-index
                  index-text='#'
                  border
                  :show-row-hover='false'
                  class="treeTable">
        <!-- slot指定模板名称 是否有效-->
        <template slot="isOk"
                  slot-scope="scope">
          <i class="el-icon-success"
             v-if="scope.row.cat_deleted === false"
             style="color:lightgreen"></i>
          <i class="el-icon-error"
             v-else
             style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order"
                  slot-scope="scope">
          <el-tag size="mini"
                  v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success"
                  size="mini"
                  v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag type="warning"
                  size="mini"
                  v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt"
                  slot-scope="scope">
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini">编辑</el-button>
          <el-button type="danger"
                     icon="el-icon-delete"
                     size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pageNum"
                     :page-sizes="[3, 5, 10, 15]"
                     :page-size="queryInfo.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类"
               :visible.sync="addCateDialogVisible"
               width="50%"
               @close="addCateDialogClosed">
      <!-- 添加分类的表单 -->
      <el-form :model="addCateForm"
               :rules="addCateFormRules"
               ref="addCateFormRef"
               label-width="100px">
        <el-form-item label="分类名称："
                      prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader expand-trigger="hover"
                       v-model="selectedOptions"
                       clearable
                       change-on-select
                       :options="parentCateList"
                       :props="cascaderProps"
                       @change="parentCateChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 查询条件
      queryInfo: {
        type: 3,
        pageNum: 1,
        pageSize: 5
      },
      // 商品分类数据列表，默认为空
      cateList: [{
        cat_name: '电视',
        cat_deleted: false,
        cat_level: 0,
        children: [{
          cat_name: '大电视',
          cat_deleted: false,
          cat_level: 1
        }, {
          cat_name: '小电视',
          cat_deleted: false,
          cat_level: 1
        }]
      }, {
        cat_name: '冰箱',
        cat_deleted: true,
        cat_level: 0,
        children: [{
          cat_name: '双桶洗衣机',
          cat_deleted: false,
          cat_level: 1,
          children: [{
            cat_name: '大的',
            cat_deleted: false,
            cat_level: 2
          }, {
            cat_name: '晓得',
            cat_deleted: false,
            cat_level: 2
          }]
        }]
      }, {
        cat_name: '洗衣机',
        cat_deleted: true,
        cat_level: 0
      }, {
        cat_name: '手机相机',
        cat_deleted: false,
        cat_level: 0,
        children: [{
          cat_name: '大的',
          cat_deleted: false,
          cat_level: 2
        }, {
          cat_name: '晓得',
          cat_deleted: false,
          cat_level: 2
        }]
      }],
      // 总数据条数
      total: 30,
      // 为table指定列的定义
      column: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template', // 将当前列指定为模板列
        template: 'isOk'// 当前这一列使用的模板名称
      }, {
        label: '排序',
        type: 'template', // 将当前列指定为模板列
        template: 'order'// 当前这一列使用的模板名称
      }, {
        label: '操作',
        type: 'template', // 将当前列指定为模板列
        template: 'opt'// 当前这一列使用的模板名称
      }],
      // 控制添加分类对话框的显示与隐藏
      addCateDialogVisible: false,
      // 添加分类的数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 添加分类表单的验证规则对象
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [{
        cat_id: 1,
        cat_name: '美妆洗护',
        children: [
          {
            cat_id: 11,
            cat_name: '洗发水'
          },
          {
            cat_id: 12,
            cat_name: '护发素'
          },
          {
            cat_id: 13,
            cat_name: '隔离'
          }
        ]
      }, {
        cat_id: 2,
        cat_name: '珠宝首饰',
        children: [
          {
            cat_id: 21,
            cat_name: '黄金珠宝'
          },
          {
            cat_id: 22,
            cat_name: '首饰配饰'
          },
          {
            cat_id: 23,
            cat_name: '品牌墙'
          }
        ]
      }],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedOptions: []
    }
  },
  created () {
    // this.getCateList()
  },
  methods: {
    // 获取商品分类数据
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听pageSize改变
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      // this.getCateList()
    },
    // 监听pageNum的改变
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      // this.getCateList()
    },
    // 点击按钮展示添加分类对话框
    showAddCateDialog () {
      // 获取父级分类列表
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    // 获取父级分类的数据列表
    async getParentCateList () {
      // const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      // if (res.meta.status !== 200) {
      //   return this.$message.error('获取数据失败')
      // }
      // this.parentCateList = res.data
    },
    // 选择项发生改变触发
    parentCateChange () {
      if (this.selectedOptions.length > 0) {
        // 选中父级分类
        this.addCateForm.cat_pid = this.selectedOptions[this.selectedOptions.length - 1]
        this.addCateForm.cat_level = this.selectedOptions.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 点击按钮添加新得分类
    addCate () {
      console.log(this.addCateForm)
      this.addCateDialogVisible = false
    },
    // 监听对话框的关闭事件
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedOptions = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>
<style scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
